package com.icoolkj.ms.auth.controller;

import com.icoolkj.ms.auth.dto.RefreshTokenDTO;
import com.icoolkj.ms.auth.manager.TokenManager;
import com.icoolkj.ms.api.auth.vo.TokenInfoVO;
import com.icoolkj.ms.common.core.response.ServerResponseEntity;
import com.icoolkj.ms.common.core.utils.BeanUtil;
import com.icoolkj.ms.common.security.bo.TokenInfoBO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/02/15 15:56
 */
@RestController
public class TokenController {

    @Autowired
    private TokenManager tokenManager;

    @PostMapping("/public/token/refresh")
    public ServerResponseEntity<TokenInfoVO> refreshToken(@Valid @RequestBody RefreshTokenDTO refreshTokenDTO) {
        ServerResponseEntity<TokenInfoBO> tokenInfoBOServerResponseEntity =  tokenManager.refreshToken(refreshTokenDTO.getRefreshToken());
        if (!tokenInfoBOServerResponseEntity.isSuccess()){
            return ServerResponseEntity.transform(tokenInfoBOServerResponseEntity);
        }

        return ServerResponseEntity.success(BeanUtil.map(tokenInfoBOServerResponseEntity.getData(), TokenInfoVO.class));
    }

}
